<?php

class Application_Model_ProductBuyMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_ProductBuy');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_ProductBuy $obj)
    {
        $data = array(
            'member_id' => $obj->getMember_id(),
        	'title'   => $obj->getTitle(),
        	'description'   => $obj->getDescription(),
        	'price'   => $obj->getPrice(),
        	'quantity' => $obj->getQuantity(),
        	'image' => $obj->getImage(),
        	'sub_image1' => $obj->getSub_image1(),
        	'sub_image2' => $obj->getSub_image2(),
        	'country' => $obj->getCountry(),
        	'category' => $obj->getCategory(),
            'new' => $obj->getNew(),
        	'day'   => $obj->getDay(),
        	'trade' => $obj->getTrade(),
        	'deposit' => $obj->getDeposit(),
        	'delivery' => $obj->getDelivery(),
        	'paypal' => $obj->getPaypal(),
	        'bank' => $obj->getBank(),
	        'cheque' => $obj->getCheque(),
	        'others' => $obj->getOthers(),
        	'status'   => $obj->getStatus(),
        	'winner'   => $obj->getWinner(),
        	'creation_time' => $obj->getCreation_time()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_ProductBuy $obj)
    {
    	$db = $this->getDbTable();
    	
         $data = array(
            'member_id' => $obj->getMember_id(),
        	'title'   => $obj->getTitle(),
        	'description'   => $obj->getDescription(),
         	'price'   => $obj->getPrice(),
         	'quantity' => $obj->getQuantity(),
         	'image' => $obj->getImage(),
         	'sub_image1' => $obj->getSub_image1(),
        	'sub_image2' => $obj->getSub_image2(),
         	'country' => $obj->getCountry(),
         	'category' => $obj->getCategory(),
            'new' => $obj->getNew(),
        	'day'   => $obj->getDay(),
        	'trade' => $obj->getTrade(),
        	'deposit' => $obj->getDeposit(),
        	'delivery' => $obj->getDelivery(),
        	'paypal' => $obj->getPaypal(),
	        'bank' => $obj->getBank(),
	        'cheque' => $obj->getCheque(),
	        'others' => $obj->getOthers(),
        	'status'   => $obj->getStatus(),
         	'winner'   => $obj->getWinner(),
        	'creation_time' => $obj->getCreation_time()
        );
        $db->update($data,array('product_buy_id = ?' => $obj->getProduct_buy_id()));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_ProductBuy();
			$entry->setProduct_buy_id($row->product_buy_id)
				->setMember_id($row->member_id)
				->setTitle($row->title)
				->setDescription($row->description)
				->setPrice($row->price)
				->setQuantity($row->quantity)
				->setImage($row->image)
				->setSub_image1($row->sub_image1)
				->setSub_image2($row->sub_image2)
				->setCountry($row->country)
				->setCategory($row->category)
				->setNew($row->new)
				->setDay($row->day)
				->setTrade($row->trade)
				->setDeposit($row->deposit)
				->setDelivery($row->delivery)
				->setPaypal($row->paypal)
				->setBank($row->bank)
				->setCheque($row->cheque)
				->setOthers($row->others)
				->setStatus($row->status)
				->setWinner($row->winner)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findByResultWithUsername($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_ProductBuy();
			$entry->setProduct_buy_id($row->product_buy_id)
				->setMember_id($row->member_id)
				->setUsername($row->username)
				->setTitle($row->title)
				->setDescription($row->description)
				->setPrice($row->price)
				->setQuantity($row->quantity)
				->setImage($row->img)
				->setSub_image1($row->sub_image1)
				->setSub_image2($row->sub_image2)
				->setCountry($row->country)
				->setCategory($row->category)
				->setNew($row->new)
				->setDay($row->day)
				->setTrade($row->trade)
				->setDeposit($row->deposit)
				->setDelivery($row->delivery)
				->setPaypal($row->paypal)
				->setBank($row->bank)
				->setCheque($row->cheque)
				->setOthers($row->others)
				->setStatus($row->status)
				->setWinner($row->winner)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findAll()
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('product_buy')
        	->order('creation_time DESC');
    	return $this->findByResult($select);
    }
    
	public function findByProduct_buy_id($product_buy_id)
    {
    	if($product_buy_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy')
	             ->where('product_buy_id = ?', $product_buy_id);
				
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberId($member_id)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	    	
	    	$select = $db->select()
	    	->from(array('b'=>'member'))
			->join(array('a'=>'product_buy'), 'b.member_id = a.member_id', array('product_buy_id', 'member_id' => 'b.username', 'title', 'description', 'price', 'quantity',
				'image' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
				'others', 'status', 'winner', 'category', 'creation_time'))
			->where('a.member_id = ?', $member_id)
			->order('product_buy_id DESC');
			
			$select->setIntegrityCheck(false); 
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberIdPbId($member_id, $product_buy_id)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy')
	             ->where('member_id = ?', $member_id)
	             ->where('product_buy_id = ?', $product_buy_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberIdCreationTime($member_id, $time)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy')
	             ->where('member_id = ?', $member_id)
	             ->where('creation_time = ?', $time);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByProductName($name)
    {
    	$db = $this->getDbTable();

        $select = $db->select()
			->from(array('a'=>'product_buy'), array('product_buy_id', 'member_id' => 'b.username', 'title', 'description', 'price', 'quantity',
				'img' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
				'others', 'status', 'winner', 'category', 'creation_time'))
			->join(array('b'=>'member'),'b.member_id = a.member_id')
			->where('LOWER(title) LIKE ?', '%' . strtolower($name) . '%')
			->where('status = "B"');
			
		$select->setIntegrityCheck(false); 
    	
    	return $this->findByResultWithUsername($select);
    }
    
	public function findByNameCategoryCountry($name, $category, $country, $type)
    {
    	$db = $this->getDbTable();

    	if($type == "all"){
	        $select = $db->select()
				->from(array('a'=>'product_buy'), array('product_buy_id', 'member_id' => 'b.username', 'title', 'description', 'price', 'quantity',
					'img' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
					'others', 'status', 'winner', 'category', 'creation_time'))
				->join(array('b'=>'member'),'b.member_id = a.member_id')
				->where('LOWER(title) LIKE ?', '%' . strtolower($name) . '%')
				->where('category = ?', $category)
				->where('country = ?', $country)
				->where('status = "B"');
    	}
    	else if($type == "category"){
    		 $select = $db->select()
				->from(array('a'=>'product_buy'), array('product_buy_id', 'member_id' => 'b.username', 'title', 'description', 'price', 'quantity',
					'img' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
					'others', 'status', 'winner', 'category', 'creation_time'))
				->join(array('b'=>'member'),'b.member_id = a.member_id')
				->where('LOWER(title) LIKE ?', '%' . strtolower($name) . '%')
				->where('category = ?', $category)
				->where('status = "B"');
    	}
    	else if($type == "country"){
    		 $select = $db->select()
				->from(array('a'=>'product_buy'), array('product_buy_id', 'member_id' => 'b.username', 'title', 'description', 'price', 'quantity',
					'img' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
					'others', 'status', 'winner', 'category', 'creation_time'))
				->join(array('b'=>'member'),'b.member_id = a.member_id')
				->where('LOWER(title) LIKE ?', '%' . strtolower($name) . '%')
				->where('country = ?', $country)
				->where('status = "B"');
    	}
			
		$select->setIntegrityCheck(false); 
    	
    	return $this->findByResultWithUsername($select);
    }
    
	public function findByPbBidMemberId($member_id)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	    	
	    	$select = $db->select()
	    	->from(array('c'=>'product_buy_bid'))
			->join(array('a'=>'product_buy'), 'c.product_buy_id = a.product_buy_id', array('product_buy_id','title', 'description', 'price', 'quantity',
				'image' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
				'others', 'status', 'winner', 'category', 'creation_time'))
			->join(array('b'=>'member'),'a.member_id = b.member_id', array('member_id' => 'b.username'))
			->where('c.member_id = ?', $member_id)
			->group('c.product_buy_id');
			
			$select->setIntegrityCheck(false); 
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByDiscountMemberId($member_id)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	    	
	    	$select = $db->select()
			->from(array('a'=>'product_buy'), array('product_buy_id', 'title', 'description', 'price', 'quantity',
				'image' => 'a.image', 'sub_image1', 'sub_image2', 'country', 'new', 'day', 'trade', 'deposit', 'delivery', 'paypal', 'bank', 'cheque',
				'others', 'status', 'winner', 'category', 'creation_time'))
			->join(array('b'=>'product_buy_bid'),'b.product_buy_id = a.product_buy_id and b.member_id = a.winner',  array('final_price' => 'b.price'))
			->join(array('c'=>'member'),'a.winner = c.member_id')
			->where('a.member_id = ?', $member_id)
			->where('a.status = "F"');
			
			$select->setIntegrityCheck(false); 
	    	
	    	$db = $this->getDbTable();
	    	$resultSet = $db->fetchAll($select);
	        $entries   = array();
			foreach ($resultSet as $row) {
				$entry = new Application_Model_ProductBuy();
				$entry->setProduct_buy_id($row->product_buy_id)
					->setMember_id($row->member_id)
					->setTitle($row->title)
					->setDescription($row->description)
					->setPrice($row->price)
					->setQuantity($row->quantity)
					->setImage($row->image)
					->setSub_image1($row->sub_image1)
					->setSub_image2($row->sub_image2)
					->setCountry($row->country)
					->setCategory($row->category)
					->setNew($row->new)
					->setDay($row->day)
					->setTrade($row->trade)
					->setDeposit($row->deposit)
					->setDelivery($row->delivery)
					->setPaypal($row->paypal)
					->setBank($row->bank)
					->setCheque($row->cheque)
					->setOthers($row->others)
					->setStatus($row->status)
					->setWinner($row->winner)
					->setCreation_time($row->creation_time)
					->setFinal_price($row->final_price);
				$entries[] = $entry;
			}
			return $entries;
    	}
    }
    
    public function findByWinnerId($member_id){
    	if($member_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy')
	             ->where('winner = ?', $member_id);
				
	    	return $this->findByResult($select);
    	}
	}
}

